<?php
/**
 * Created by PhpStorm.
 * User: lg
 * Date: 2017/12/8
 * Time: 11:27
 */

namespace app\common\rbac;


use think\Exception;

class Auth
{
    /**
     * @var string  权限表
     */
    protected $permission_table = 'permission';
    /**
     * @var string  角色表
     */
    protected $role_table = 'role';
    /**
     * @var string  用户表
     */
    protected $user_table = 'user';
    /**
     * @var string  用户角色关联表
     */
    protected $user_role_table = 'user_role';
    /**
     * @var string  角色权限关联表
     */
    protected $role_permission_table = 'role_permission';


    public function __construct($data = [])
    {
        if (isset($data['permission']))
            $this->permission_table = $data['permission'];
        if (isset($data['role']))
            $this->role_table = $data['role'];
        if (isset($data['user']))
            $this->user_table = $data['user'];
        if (isset($data['user_role']))
            $this->user_role_table = $data['user_role'];
        if (isset($data['role_permmission']))
            $this->role_permission_table = $data['role_permmission'];
    }

    /**
     * 创建角色
     */
    public function crateRole($data)
    {
        return db($this->role_table)->insert($data);
    }

    /**
     * 创建权限
     */
    public function createPermission($data)
    {
        return db($this->permission_table)->insert($data);
    }

    /**
     * 获取权限列表
     * @param $where
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getPermession($where = [])
    {
        return db($this->permission_table)->where($where)->select();
    }

    /**
     * 设置角色权限
     */
    public function setRolePermission($id = 0, $data = [])
    {
        db()->startTrans();
        $dbObj = db($this->role_permission_table);
        try {
            if (false === $dbObj->where(['role_id' => $id])->delete()) {
                db()->rollback();
                return false;
            };
            $all = [];
            foreach ($data as $v) {
                $all[] = ['role' => $id, 'permission_id' => $v];
            }
            $dbObj->insertAll($all);

        } catch (Exception $e) {
            db()->rollback();
            return false;
        }
        db()->commit();
        return true;
    }

    /**
     * 设置用户角色
     */
    public function setUserRole()
    {

    }

    /**
     * 缓存用户权限
     * @param $user_id
     */
    public function cachePermession($user_id)
    {
        $role_id = db($this->user_role_table)->where(['admin_id' => $user_id])->find();
        $permession = db($this->role_permission_table)->where(['role_id' => $role_id['role_id']])->column('permession_id');
        session('auth.permession', $permession);
        return true;
    }

    /**
     * 用户权限检测
     * @param $action 操作名称
     * @return bool
     */
    public function check($action)
    {
        $permession = session('auth.permession');
        if (!in_array($action, $permession)) {
            return false;
        }
        return true;
    }
}